/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2012-6-28         luwei
 */

package com.tianci.appstore.ui.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

import com.tianci.appstore.R;

public class SkyThreePagerFlipper extends ViewFlipper {

    public static final int RIGHT_DIRECTION = KeyEvent.KEYCODE_DPAD_RIGHT;
    public static final int LEFT_DIRECTION = KeyEvent.KEYCODE_DPAD_LEFT;
    private int displayViewPos = 0;
    private int displayDataPos = 0;
    private int dataCount = 0;
    private int viewCount = 0;
    private Context context = null;
    private IFlipperUpdateData updataData = null;
    private IFlipperControl focuseControl = null;
    private Animation inTopDown = null;
    private Animation inDownTop = null;
    private Animation outTopDown = null;
    private Animation outDownTop = null;


    public interface IFlipperControl {
        public void controlFocuse(int viewNum, boolean focuse);

        public void controlKeyEffective(boolean effective);
    }

    public interface IFlipperUpdateData {
        public void changeData(int fPos, int sPos, int tPos);
    }

    public SkyThreePagerFlipper(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public void initFlipper(Context context, int dataCount, IFlipperUpdateData updataData) {
        this.updataData = updataData;
        this.dataCount = dataCount;
        this.context = context;
        inTopDown = AnimationUtils.loadAnimation(context, R.anim.in_rightleft);
        inDownTop = AnimationUtils.loadAnimation(context, R.anim.in_leftright);
        outDownTop = AnimationUtils.loadAnimation(context, R.anim.out_leftright);
        outTopDown = AnimationUtils.loadAnimation(context, R.anim.out_rightleft);
        inTopDown.setAnimationListener(animListener);
        inDownTop.setAnimationListener(animListener);
        outDownTop.setAnimationListener(outListener);
        outTopDown.setAnimationListener(outListener);
        System.out.println("<===the pages NUM of the flipper is : " + dataCount + "===>");

    }

    public void setViewCount(int count) {
        this.viewCount = count;
    }

    public void reSet(int pageNum, int viewNum) {
        this.displayDataPos = pageNum;
        this.displayViewPos = viewNum;
    }

    public void setDataCount(int dataCount) {

        this.dataCount = dataCount;
    }

    public void setCurrentDataNum(int num) {
        displayDataPos = num;
    }

    public void setFocuseControl(IFlipperControl focuseControl) {
        this.focuseControl = focuseControl;
    }

    AnimationListener outListener = new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            if (focuseControl != null) {
                focuseControl.controlKeyEffective(false);
            }
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {
        }
    };

    AnimationListener animListener = new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            if (focuseControl != null) {
                focuseControl.controlKeyEffective(false);
            }
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            Message msg = handler.obtainMessage(ANIM_END);
            handler.sendMessageDelayed(msg, 20);
        }
    };
    public static final int ANIM_END = 0;
    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case ANIM_END:
                    if (focuseControl != null) {
                        focuseControl.controlFocuse(displayViewPos % 3, true);
                        focuseControl.controlKeyEffective(true);
                    }
                    break;

                default:
                    break;
            }
        }

        ;
    };


    public void showSpecView(int direction) {
        switch (direction) {
            case RIGHT_DIRECTION:
                onKeyRight();
                break;
            case LEFT_DIRECTION:
                onKeyLeft();
                break;
            default:
                break;
        }
    }

    private void onKeyRight() {
        if (displayDataPos < dataCount - 1) {
            displayDataPos++;
        } else {
            displayDataPos = 0;
        }
        displayViewPos++;
        if (displayViewPos == viewCount) {
            displayViewPos = 0;
        }
        System.out.println("<===Current Page Num : " + displayDataPos + "===>");
        System.out.println("<===Current List Num : " + displayViewPos % viewCount + "===>");
        setOutAnimation(outTopDown);
        setInAnimation(inTopDown);
        System.out.println("right  " + (displayViewPos) % viewCount);
        this.setDisplayedChild(displayViewPos % viewCount);
        notifyDataSetChanged(displayDataPos, displayViewPos);
    }

    private void onKeyLeft() {
        if (displayDataPos > 0) {
            displayDataPos--;
        } else {
            displayDataPos = dataCount - 1;
        }
        //保证不为负数
        if (displayViewPos > 0) {
            displayViewPos--;
        } else if (displayViewPos == 0) {
            displayViewPos = viewCount - 1;
        }
        System.out.println("<===Current Page Num : " + displayDataPos + "===>");
        System.out.println("<===Current List Num : " + (displayViewPos) % viewCount + "===>");
        setInAnimation(inDownTop);
        setOutAnimation(outDownTop);
        System.out.println("left  " + (displayViewPos) % viewCount);
        this.setDisplayedChild((displayViewPos) % viewCount);
        notifyDataSetChanged(displayDataPos, displayViewPos);
    }

    public int getCurrentDisplayViewNum() {
        return displayViewPos;
    }

    public int getCurrentDisplayPageNum() {
        return displayDataPos;
    }

    private void notifyDataSetChanged(int displayDataPos, int displayViewPos) {
        switch ((displayViewPos) % viewCount) {
            case 0:
                updataData.changeData(displayDataPos, displayDataPos + 1, displayDataPos - 1);
                break;
            case 1:
                updataData.changeData(displayDataPos - 1, displayDataPos, displayDataPos + 1);
                break;
            case 2:
                updataData.changeData(displayDataPos + 1, displayDataPos - 1, displayDataPos);
                break;
            default:
                break;
        }
    }

}
